package com.hxkj.service.article.impl;

import com.hxkj.client.service.AdvertisementColumnServiceClient;
import com.hxkj.domain.enums.ColumnExceptionEnum;
import com.hxkj.domain.po.AdvertisementColumn;
import com.hxkj.domain.po.AdvertisementColumnTree;
import com.hxkj.service.article.AdvertisementColumnService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 分类service实现类,广告分类的增删改查
 *
 * @author chenfangzhou 2018/09/14
 */

@Service
@Slf4j
public class AdvertisementColumnServiceImpl implements AdvertisementColumnService {

    @Autowired
    private AdvertisementColumnServiceClient advertisementColumnServiceClient;

    /**
     * 保存广告分类
     *
     * @param column
     * @return
     * @throws Exception
     */
    @Override
    public void saveColumn(AdvertisementColumn column) throws Exception {

        ResultDataStruct resultDataStruct;
        try {
            if (column == null) {
                throw new CustomMessageException(401, "分类为空!");
            }
            if (null == column.getColumnName() || column.getColumnName() == "") {
                throw new CustomMessageException(401, "分类名称为空!");
            }
            if (column.getSiteId() == null) {
                throw new CustomMessageException(401, "站点id为空!");
            }
            if (column.getColumnTemplateId() == null) {
                throw new CustomMessageException(401, "分类列表模板为空!");
            }
            if (column.getDetailTemplateId() == null) {
                throw new CustomMessageException(401, "分类详情模板为空!");
            }
            if (column.getParentColumnId() == null) {
                throw new CustomMessageException(401, "父分类id为空!");
            }

            column.setCreateTime(System.currentTimeMillis());
            column.setUpdateTime(System.currentTimeMillis());
            resultDataStruct = advertisementColumnServiceClient.saveColumn(column);
            if (resultDataStruct.getCode() != 200) {
                throw new CustomMessageException(resultDataStruct.getCode(), resultDataStruct.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 获取广告分类树列表
     * @param column
     * @return
     * @throws Exception
     */
    @Override
    public List<AdvertisementColumnTree> getColumnTreeList(AdvertisementColumn column) throws Exception {
        List<AdvertisementColumnTree> columnTreeList = new ArrayList<AdvertisementColumnTree>();
        try {
            // 判断是否是关键字搜索
            if (StringUtils.isNotBlank(column.getKeyword())) {
                columnTreeList = advertisementColumnServiceClient.getColumnTreeListByKeyword(column);
            } else {
                columnTreeList = advertisementColumnServiceClient.getColumnTreeList(column);
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取分类失败");
        }
        return columnTreeList;
    }

    /**
     * 删除分类,软删除
     * @param column
     * @throws Exception
     */
    @Override
    public void removeColumn(AdvertisementColumn column) throws Exception {
        try {
            if (column == null) {
                throw new CustomMessageException(401, "分类为空!");
            }
            if (StringUtils.isBlank(column.getColumnIds())) {
                throw new CustomMessageException(401, "分类id字符串为空!");
            }
            ResultMessageStruct resultMessageStruct = advertisementColumnServiceClient.removeColumn(column);
            if (resultMessageStruct.getCode() != 200) {
                throw new CustomMessageException(resultMessageStruct.getCode(), resultMessageStruct.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();

            throw e;
        }
    }

    /**
     * 修改分类
     * @param column
     * @throws Exception
     */
    @Override
    public void modifyColumn(AdvertisementColumn column) throws Exception {
        try {
            if (column == null) {
                throw new CustomMessageException(401, "分类为空!");
            }
            if (column.getColumnId() == null) {
                throw new CustomMessageException(401, "分类id为空!");
            }
            if (column.getSiteId() == null) {
                throw new CustomException(ColumnExceptionEnum.COLUMN_NOT_SITEID);
            }
            column.setUpdateTime(System.currentTimeMillis());
            ResultMessageStruct resultMessageStruct = advertisementColumnServiceClient.modifyColumn(column);

            if (resultMessageStruct.getCode() != 200) {
                throw new CustomMessageException(resultMessageStruct.getCode(), resultMessageStruct.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 修改广告分类的排序
     * @param map
     * @throws Exception
     */
    @Override
    public void modifySortNum(Map<String, Object> map) throws Exception {
        try {
            if (map.get("siteId") == null) {
                throw new CustomException(ColumnExceptionEnum.COLUMN_NOT_SITEID);
            }
            ResultMessageStruct resultMessageStruct = advertisementColumnServiceClient.modifySortNum(map);
            if (resultMessageStruct.getCode() != 200) {
                throw new CustomMessageException(resultMessageStruct.getCode(), resultMessageStruct.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }


    /**
     * 获取广告的最顶级分类
     * @param column
     * @return
     * @throws Exception
     */
    @Override
    public List<AdvertisementColumn> getTopParentColumn(AdvertisementColumn column) throws Exception {
        return advertisementColumnServiceClient.getTopParentColumn(column);
    }

}
